/*
 * Copyright (C) 2011 PickleMan <admin.aegamesi@gmail.com>
 * Copyright (C) 2011 IsmAvatar <IsmAvatar@gmail.com>
 * 
 * This file is part of Jeie.
 * Jeie is free software and comes with ABSOLUTELY NO WARRANTY.
 * See LICENSE for details.
 */

package org.jeie.gui.panels;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JPanel;

import org.jeie.main.EasyImageEditor;
import org.jeie.messages.Messages;

public class ColorPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = -2214015788425392678L;

	private JButton fore = new JButton();
	private JButton back = new JButton();
	private Color foreColor = Color.black;
	private Color backColor = Color.white;

	public ColorPanel() {
		setPreferredSize(new Dimension(48, 32));
		setLayout(null);

		fore.setBounds(0, 0, 24, 32);
		back.setBounds(24, 0, 24, 32);
		fore.setToolTipText(Messages.getString("Toolbox.get_foreground_color")); //$NON-NLS-1$
		back.setToolTipText(Messages.getString("Toolbox.get_background_color")); //$NON-NLS-1$
		fore.addActionListener(this);
		back.addActionListener(this);
		fore.setBackground(foreColor);
		back.setBackground(backColor);
		add(fore);
		add(back);
	}

	public Color getForeground() {
		return foreColor;
	}

	public Color getBackground() {
		return backColor;
	}

	public void setForegroundColor(Color c) {
		foreColor = c;
		fore.setBackground(c);
	}

	public void setBackgroundColor(Color c) {
		backColor = c;
		back.setBackground(c);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == fore) {
			Color newColor = JColorChooser.showDialog(
				EasyImageEditor.frame,
				Messages.getString("ColorPanel.CHOOSE_FG"), foreColor); //$NON-NLS-1$
			if (newColor != null) {
				setForegroundColor(newColor);
			}
		}
		if (e.getSource() == back) {
			Color newColor = JColorChooser.showDialog(
				EasyImageEditor.frame,
				Messages.getString("ColorPanel.CHOOSE_BG"), backColor); //$NON-NLS-1$
			if (newColor != null) {
				setBackgroundColor(newColor);
			}
		}
	}
}
